/*
 * initElementsShow(destinationClass, sourceClass)
 * destinationClass - stringas, nurodo bloko klase i kuria bus spausdinami checkboxai
 * sourceClass - stringas, nurodo elementu klase, kurie(visas html blokas) bus rodomi/slepiami.
 *		 taipogi turi buti norudytas 'for' atributas spausdinamam tekstui
 *		 id unikalus visam dokumentui kiekvienam sourceClass elementui
 */

/*
 * function initArrayShow(destinationClass, elements, functionToAct) {
 * destinationClass - stringas, nurodo bloko klase i kuria bus spausdinami checkboxai
 * elements - array su elementu pavadinimais
 * functionToAct - js funkcija kuri vykdoma padarius pakeitima (pazymejus/atzymejus checkboxa)
 */

//nurodo defaultine klase blokui i kuri bus spausdinamas chekboxu listas
defaultDestinationClass = "ElementsShow";
//nurodo defaultine klase, pagal kuria bus renkami elementai
defaultSourceClass = "ElementShown";

function initElementsShow(destinationClass, sourceClass) {
    if(destinationClass == null) destinationClass = defaultDestinationClass;
    if(sourceClass == null) destinationClass = defaultSourceClass;

    var elements = new Array();
    elements = getSourceElements(sourceClass);
    var vElements = new Array();
    vElements = getVisibleElements(sourceClass);

    html = "<ul style='list-style-type:none'>";
    $.each(elements, function(index, value) {
        html += "<li><input type='checkbox' ";
        html += "name ='in"+destinationClass+"' ";
        html += "value = '" + $(value).attr('id') + "' ";
        if(vElements.indexOf(value) > -1) {
            html += " checked ";
        }
        html += "/>";
        html += $(value).attr('for');
        html += "</li>";
    })
    //html += "<li><input type='checkbox' id='all' onclick='checkAll(\""+destinationClass+"\");'>Viską</input></li>";
    html += "</ul>";

    $("."+destinationClass).html(html);

    $("."+destinationClass+" input").click(function() {
        var display = 'none';
        if($(this).attr("checked")) {
            display = 'block';
        }
        $("."+sourceClass+"[id="+$(this).attr("value")+"]").css('display', display);
        if ($("."+destinationClass+" input:checked").length > 0) {
            document.getElementById("loadViewInfo").innerHTML = "<span class='predicted'>Tamsesniame fone</span> rodoma prognozuojama apkrova.";
        } else {
            document.getElementById("loadViewInfo").innerHTML = "Pasirinkite diagramą iš Rodyti kurią norėtumėte matyti.";
        }
    });
}

function checkAll(dc) {
    checked = $("."+dc+" #all").attr("checked");
    //if($("."+dc+" #all").attr("checked")) {
    //checked = true;
    //}
    //alert(checked);
    $("."+dc+" input").each(function() {
        //alert($(this).attr('checked'));
        if($(this).attr('checked') != checked) {
            //alert("do it");
            //$(this).attr('checked', checked);
            if($(this).attr('id') != "all"){
                alert("triger");
                $(this).trigger('click');
            }
        }
    });
}

function initArrayShow(destinationClass, elements, functionToAct) {
    html = "<ul style='list-style-type:none'>";
    $.each(elements, function(index, value) {
        html += "<li><input type='checkbox' ";
        html += "name ='in"+destinationClass+"' ";
        html += "value = '" + value + "' checked />";
        html += value;
        html += "</li>";
    })
    //html += "<li><input type='checkbox' id='all' onclick='checkAll(\""+destinationClass+"\");'>Viską</input></li>";
    html += "</ul>";

    $("."+destinationClass).html(html);

    $("."+destinationClass+" input").click(function() {
        functionToAct();
    });
}

function getSourceElements(sourceClass) {
    if(sourceClass == null) sourceClass = defaultSourceClass;
    var elements = new Array();
    $("."+sourceClass).each(function() {
        elements.push(this);
    })
    return elements;
}

function getVisibleElements(sourceClass) {
    if(sourceClass == null) sourceClass = defaultSourceClass;
    var elements = new Array();
    $("."+sourceClass).each(function() {
        if($(this).is(":visible")) elements.push(this);
    })
    return elements;
}

function getSelectedElements(destinationClass) {
    if(sourceClass == null) sourceClass = defaultDestinationClass;
    var selectedIds = new Array();
    $("."+destinationClass+" input").each(function() {
        if($(this).attr('checked'))
            selectedIds.push($(this).attr('value'));
    });
    return selectedIds;
}